package pos.data;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import com.csvreader.CsvReader;
import com.csvreader.CsvWriter;

import pos.domain.Product;

public class CSVProductDAO implements IProductDAO {
	
	private String fileName = "data/products.csv"; 
		
    public List selectAllProducts() {

        List searchResults = new LinkedList();
        CsvReader products;
		try {
			products = new CsvReader(fileName);
			products.readHeaders();
			
			while (products.readRecord())
			{
				Product temp = new Product();
                temp.setProductID(products.get("productID"));
                temp.setDescription(products.get("description"));
                temp.setPrice(Integer.parseInt(products.get("price")));
                searchResults.add(temp);
			}
			products.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        return searchResults;
    }




public Product select(String productOID) {
                return null;
    }

    public String selectProductOID(String productid) {
                
    	String result = new String("not-found-product");
    
    	CsvReader products;
		try {
			products = new CsvReader(fileName);
			products.readHeaders();
			boolean notfound = true;
			while (products.readRecord() & notfound)
			{
				String currentpid = products.get("productID");
				if(currentpid.equalsIgnoreCase(productid)){
					result = currentpid;
					notfound = false;
				}
                
			}
			products.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	
    	return result;
    }

}
